const util = require('../../../../utils/util.js')
import { save } from '../../../../api/account'
import { list } from '../../../../api/bankInfo'

const app = getApp()
Page({
    data: {
        payCode: '',
        payCodeFlag: true,
        array:[],
        index: 0,
        bankInfoId: -1,
        paymentType: '',
        bankName: '',
        bankNum: '',
        accountTypeplaceholder:'请输入本人微信账号',
        bankInfoList:'',
        code:'',
    },
    onLoad: function (options) {
        this.getBankInfo()
    },
    getBankInfo(){
        list({mchId:app.globalData.mchId}).then(res=>{
            this.data.bankInfoList = res.data.data
            this.setData({
                bankInfoList:  this.data.bankInfoList
            })
            this.data.array = [] 
            this.data.bankInfoList.forEach(item=>{
                this.data.array.push(item.name)
            })
            if (this.data.bankInfoList.length>0) {
                this.data.paymentType = this.data.bankInfoList[0].name;
                this.data.bankInfoId = this.data.bankInfoList[0].id;
                this.data.code = this.data.bankInfoList[0].code;
                if(this.data.bankInfoId == 0) {
                    this.data.accountTypeplaceholder= "请输入本人身证份号"
                }else if(this.data.bankInfoId == 1) {
                    this.data.accountTypeplaceholder= "请输入本人微信账号"
                }else if(this.data.bankInfoId == 2){
                    this.data.accountTypeplaceholder= "请输入本人支付宝账号"
                }else{
                    this.data.accountTypeplaceholder= "请输入本人卡号"
                }    
                this.setData({
                    index: 0,
                    paymentType: this.data.paymentType,
                    bankInfoId: this.data.bankInfoId,
                    code: this.data.code,
                    accountTypeplaceholder:this.data.accountTypeplaceholder
                })
            }
            this.setData({
                array:this.data.array
            })

        })
    },
    getInfoClick() {
        var _this = this
        _this.data.payCode = ''
        getInfo().then(res => {
            if (res.data.member.payCode) {
                _this.data.payCode = res.data.member.payCode
            }
            _this.setData({
                payCode: _this.data.payCode
            })
        })
    },
    metaClick(data) {
        this.data.payCode = data.detail.images[0]
        if (this.data.payCode) {
            console.log("payCodeFlag我进来了")
            this.data.payCodeFlag = false
            this.setData({
                payCodeFlag: this.data.payCodeFlag
            })
        }
    },
    qrcodeClick() {
        wx.previewImage({"current":0,"urls":[paymentType.code]})
    },
    payCodeClick() {
        if (this.data.payCode == "") {
            wx.showToast({
                title: '请先上传二维码',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        var _this = this
        if (_this.data.payCode) {
            updatePayCode({ payCode: _this.data.payCode }).then(res => {
                wx.showToast({
                    title: "操作成功",
                    icon: 'none',
                    duration: 3000,
                    success: function () {
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 1000)
                    }
                })
            })
        } else {
            wx.showToast({
                title: "请上传收款码",
                icon: 'none',
                duration: 3000,
                success: function () {
                }
            })
        }
    },
    imgClick() {
        this.selectComponent('#upload').text();
    },
    bindPickerChange: function (e) {
        var _this = this
        this.data.paymentType = _this.data.array[e.detail.value]
        this.data.bankInfoList.forEach(item=>{
            if(item.name == this.data.paymentType){
                this.data.bankInfoId = item.id
                this.data.code = item.code
                if(item.id == 0) {
                    this.data.accountTypeplaceholder= "请输入本人身证份号"
                }else if(item.id == 1) {
                    this.data.accountTypeplaceholder= "请输入本人微信账号"
                }else if(item.id == 2){
                    this.data.accountTypeplaceholder= "请输入本人支付宝账号"
                }else{
                    this.data.accountTypeplaceholder= "请输入本人卡号"
                }
            }
        })
        _this.setData({
            index: e.detail.value,
            paymentType: _this.data.paymentType,
            bankInfoId: _this.data.bankInfoId,
            code: _this.data.code,
            accountTypeplaceholder:_this.data.accountTypeplaceholder
        })
    },
    addbank() {
        var _this = this
        if (util.isNull(_this.data.bankName)) {
            wx.showToast({
                title: '请输入选择账号类型',
                duration: 2000,
                icon: 'none',

            })
            return;
        }
        if (util.isNull(_this.data.bankNum)) {
            wx.showToast({
                title: '请输入支付宝账号',
                duration: 2000,
                icon: 'none',
            })
            return;
        }
        if (_this.data.bankInfoId == 2 || _this.data.bankInfoId == 1) {
            if (util.isNull(_this.data.payCode)) {
                wx.showToast({
                    title: '请上传收款码',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
        }
        let params = {
            bankInfoId: this.data.bankInfoId,
            bankName: this.data.bankName,
            bankNum: this.data.bankNum,
            qrcode: this.data.payCode
        }
        console.log(params)
        save(params).then(res => {
            if (res.type = 'success') {
                wx.showToast({
                    title: '添加成功',
                    icon: 'none',
                })
                app.event.emit("onBankChange");
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 2000);
            }
        }).catch(repond => {
        })
    },
    bindKeyInput(data) {
        this.setData({
            bankName: data.detail.value
        })
    },
    bindKeyInputNum(data) {
        this.setData({
            bankNum: data.detail.value
        })
    }
})
